package com.edais.service.imp.advert;

import com.alibaba.fastjson.JSONObject;
import com.edais.domain.advert.BannerManage;
import com.edais.persistence.advert.BannerManageMapper;
import com.edais.service.SystemSettingService;
import com.edais.service.advert.BannerManageService;
import com.edais.util.Consts;
import com.edais.vo.BannerManageVo;
import com.yunzhi.domain.ResultCode;
import com.yunzhi.domain.ResultDTO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * BannerManageServiceImpl
 * Created by haoyx on 2016/12/12.
 */
@Service
public class BannerManageServiceImpl implements BannerManageService {

    private static final Logger logger = Logger.getLogger(BannerManageServiceImpl.class);

    @Autowired
    private BannerManageMapper bannerManageMapper;

    @Autowired
    private SystemSettingService systemSettingService;


    @Override
    public ResultDTO<BannerManageVo> queryBannerList(JSONObject params) throws Exception {

        //获取分页参数
        String type = String.valueOf(params.get("type"));
        int pageIndex = 1;
        int pageSize = 10;

        if(params.containsKey("pageSize")){
            pageSize = params.getInteger("pageSize");
            if(pageSize > 100) pageSize = 10;
        }

        if(params.containsKey("current")){
            pageIndex = params.getInteger("current");
            if(pageIndex < 0) pageSize = 1;
        }

        //查询礼品
        Map<String,Object> listParam = new HashMap<>();
        listParam.put("type",type);
        listParam.put("current",(pageIndex -1) * pageSize);
        listParam.put("pageSize",pageSize);
        List<BannerManage> list = bannerManageMapper.selectByType(listParam);
        int totalNum = bannerManageMapper.countByType(listParam);
        fomartImageUrl(list);

        ResultDTO<BannerManageVo> resultDTO = new ResultDTO<>(ResultCode.SUCCESS);
        resultDTO.setTotal(totalNum);
        List<BannerManageVo> voList = toBannerManageVo(list);
        resultDTO.setRecords(voList);
        resultDTO.setPageSize(pageSize);

        return resultDTO;
    }

    private void fomartImageUrl(List<BannerManage> list) throws Exception {
        if(null == list || list.isEmpty())
            return;
        //查询出前缀
        String urlPrefix = this.systemSettingService.getSystemSettingValueByName(Consts.SystemSettingUpload.DOMAIN_NAME);
        if(StringUtils.isEmpty(urlPrefix))
            return;
        for(BannerManage  bannerManage : list){
            if(StringUtils.isEmpty(bannerManage.getImageUrl()) || !bannerManage.getImageUrl().startsWith("/"))
                continue;
            bannerManage.setImageUrl(urlPrefix + bannerManage.getImageUrl());
        }
    }

    private String fomartUrl(String url,String urlPrefix) throws Exception {
        if(StringUtils.isEmpty(url) || !url.startsWith("/"))
            return url;
        if(!urlPrefix.startsWith("http")){
        	urlPrefix = "http://" + urlPrefix;
        }
        return urlPrefix + url;
    }
    
    private List<BannerManageVo> toBannerManageVo(List<BannerManage> list){
        List<BannerManageVo> result = new ArrayList<>();
        if(null == list || list.isEmpty())
            return result;
        for(BannerManage banner : list)
            result.add(new BannerManageVo(banner));
        return result;
    }

    @Override
    public ResultDTO queryAppStartBanner() throws Exception {

        BannerManage bannerManage = bannerManageMapper.queryAppStartBanner();

        if(null == bannerManage){
            return new ResultDTO(ResultCode.FAILED,"查询不到app启动页");
        }

        //查询出前缀
        String urlPrefix = this.systemSettingService.getSystemSettingValueByName(Consts.SystemSettingUpload.DOMAIN_NAME);
        bannerManage.setImageUrl(fomartUrl(bannerManage.getImageUrl(),urlPrefix));
        bannerManage.setUrl(fomartUrl(bannerManage.getUrl(), urlPrefix));

        JSONObject json = new JSONObject();
        json.put("htmlUrl",bannerManage.getUrl());
        json.put("peatureUrl",bannerManage.getImageUrl());
        json.put("time", bannerManage.getAdTime());
        json.put("title", bannerManage.getAdTitle());

        ResultDTO resultDTO = new ResultDTO(ResultCode.SUCCESS);
        resultDTO.setRecord(json);
        return resultDTO;
    }

}
